package spaska.data;

import java.util.List;

import spaska.data.Attribute.ValueType;

public class Factory {

	public static Value createValue(Object val) {
		if (val instanceof String) {
			return new NominalValue((String) val);
		}
		else if (val instanceof Number) {
			return new NumericValue(Double.parseDouble(val.toString()));
		}
		return UnknownValue.getInstance();
	}

	public static Instance createInstance(Object[] vector) {
		Value[] values = new Value[vector.length];
		for (int i = 0; i < values.length; i++) {
			values[i] = createValue(vector[i]);
		}
		return new Instance(values);
	}

	public static Value[] createValues(String[] strValues, Dataset dataset) {
		Value[] element = new Value[strValues.length];
		List<Attribute> attributes = dataset.getAttributes();
		int i = 0;
		for (Attribute a : attributes) {
			if (i < strValues.length) {
				if (a.getType().equals(ValueType.Numeric)) {
					try {
						Double val = Double.parseDouble(strValues[i]);
						element[i] = new NumericValue(val);
					}
					catch (java.lang.NumberFormatException e) {
						element[i] = UnknownValue.getInstance();
					}
				}
				else {
					if ((strValues[i].trim()).equals("?")) {
						element[i] = UnknownValue.getInstance();
					}
					else {
						element[i] = new NominalValue(strValues[i].trim());
					}
				}
			}
			else {
				element[i] = UnknownValue.getInstance();
			}
			i++;
		}
		return element;
	}
}
